package dicttype

import (
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/gogf/gf/v2/util/gconv"
	"ruoyi-vue-pro-go/api/common"
	v1 "ruoyi-vue-pro-go/api/dicttype/v1"
	"ruoyi-vue-pro-go/internal/consts"
	"ruoyi-vue-pro-go/internal/dao"
	"ruoyi-vue-pro-go/internal/model/do"
	"ruoyi-vue-pro-go/internal/model/entity"
	"ruoyi-vue-pro-go/internal/service"
	"ruoyi-vue-pro-go/utility/excel"
)

type (
	sDictType struct{}
)

func init() {
	service.RegisterDictType(New())
}

func New() service.IDictType {
	return &sDictType{}
}

func (s *sDictType) GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error) {
	res = &v1.GetOneRes{}
	err = dao.SystemDictType.Ctx(ctx).Where(do.SystemDictType{
		Id: req.Id,
	}).Scan(&res.SystemDictType)
	return
}

func (s *sDictType) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
	res = &v1.GetListRes{}
	err = dao.SystemDictType.Ctx(ctx).Scan(&res.List)
	return
}

func (s *sDictType) GetPage(ctx context.Context, req *v1.GetPageReq) (res *v1.GetPageRes, err error) {
	res = &v1.GetPageRes{
		List:          make([]*entity.SystemDictType, 0),
		CommonPageRes: &common.CommonPageRes{},
	}
	model := dao.SystemDictType.Ctx(ctx)
	if req.Name != "" {
		model = model.Where("name = ?", req.Name)
	}
	if req.Type != "" {
		model = model.Where("type = ?", req.Type)
	}
	if req.Status != -1 {
		model = model.Where("status", req.Status)
	}
	if req.PageSize != 0 {
		res.PageNo = req.PageNo
		res.PageSize = req.PageSize
		model = model.Page(req.PageNo, req.PageSize)
	}
	err = model.ScanAndCount(&res.List, &res.Total, false)
	return
}

func (s *sDictType) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
	res = &v1.UpdateRes{}
	// 这里需要获取当前用户名和租户id
	r := ghttp.RequestFromCtx(ctx)
	username := r.GetCtxVar("username").String()
	req.Creator = username
	req.Updater = username
	_, err = dao.SystemDictType.Ctx(ctx).Data(req).Where(do.SystemDictType{
		Id: req.Id,
	}).Update()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "dict type update failed")
	}
	return
}

func (s *sDictType) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
	res = &v1.CreateRes{}
	r := ghttp.RequestFromCtx(ctx)
	username := r.GetCtxVar("username").String()
	req.Creator = username
	req.Updater = username
	var cls = dao.SystemDictType.Columns()
	count, err := dao.SystemDictType.Ctx(ctx).
		Where(cls.Name, req.Name).Count()
	if err != nil {
		return res, err
	}
	if count > 0 {
		return res, gerror.NewCode(gcode.CodeDbOperationError, "字典类型已存在")
	}
	_, err = dao.SystemDictType.Ctx(ctx).Data(req).Insert()
	if err != nil {
		return res, gerror.NewCode(gcode.CodeDbOperationError, "dict type create failed")
	}
	return
}

func (s *sDictType) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
	res = &v1.DeleteRes{}
	var cls = dao.SystemDictType.Columns()
	_, err = dao.SystemDictType.Ctx(ctx).Where(cls.Id, req.Id).Delete()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "dict type delete failed")
	}
	return
}

func (s *sDictType) DeleteList(ctx context.Context, req *v1.DeleteListReq) (res *v1.DeleteListRes, err error) {
	res = &v1.DeleteListRes{}
	var cls = dao.SystemDictType.Columns()
	// 参数转换
	ids := gconv.Ints(gstr.Split(req.Ids, ","))
	if len(ids) == 0 {
		return nil, gerror.New("无效的ID参数")
	}
	_, err = dao.SystemDictType.Ctx(ctx).WhereIn(cls.Id, ids).Delete()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "dict type delete list failed")
	}
	return
}

func (s *sDictType) ExportExcel(ctx context.Context, req *v1.ExportExcelReq) (res *v1.ExportExcelRes, err error) {
	res = &v1.ExportExcelRes{}
	// 1. 获取用户数据
	dictTypeList, err := service.DictType().GetList(ctx, &v1.GetListReq{})
	if err != nil {
		return nil, gerror.Wrap(err, "获取字典类型失败")
	}

	// 2. 创建导出器
	exporter := excel.NewGenericExporter(
		"字典数据.xlsx",
		dictTypeList.List,
		excel.WithFields(consts.DictTypeExportFieldsOrder...),
		excel.WithHeaderMap(consts.DictTypeExportFields),
	)

	// 3. 执行导出
	r := ghttp.RequestFromCtx(ctx)
	if err := exporter.Export(ctx, r.Response); err != nil {
		return nil, gerror.Wrap(err, "导出字典类型列表失败")
	}

	// 4. 终止后续处理
	r.ExitAll()
	return nil, nil
}
